﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SyncLib.Client;

namespace SyncLib.Provider
{
    public class ConfigSyncProvider : BaseKeeperSync
    {
        // string configTableName = "ClientConfig";
        protected override void OnApplyingChanges(Microsoft.Synchronization.Data.DbApplyingChangesEventArgs e)
        {
            if (_SyncContext.Position == Microsoft.Synchronization.SyncProviderPosition.Local)
            {
                string colIdName = "";
                foreach (DataTable tbl in e.Changes.Tables)
                {
                    colIdName = "";
                    if (tbl.Columns.Contains("ClientID"))
                    {
                        colIdName = "ClientID";
                    }
                    if (tbl.Columns.Contains("ClientId"))
                        colIdName = "ClientId";
                    if (string.IsNullOrEmpty(colIdName))
                        continue;

                    DataView view = new DataView(tbl);
                    view.RowStateFilter = DataViewRowState.CurrentRows | DataViewRowState.Deleted;
                    object objtem = CommonVariable.Get(ClientConst.ClientID);
                    Guid clientId = Guid.Empty;
                    if (objtem != null)
                        clientId = (Guid)objtem;

                    foreach (DataRowView rowView in view)
                    {
                        if (rowView[colIdName].ToString().Equals(clientId.ToString(), StringComparison.InvariantCultureIgnoreCase))
                        {
                            continue;
                        }
                        else
                        {
                            bool callAccep = rowView.Row.RowState != DataRowState.Added;
                            rowView.Row.AcceptChanges();
                        }
                    }
                }
            }

            base.OnApplyingChanges(e);
        }
    }
}
